package cn.campus.converter;

import cn.campus.pojo.article.dto.ArticleDTO;
import cn.campus.pojo.article.dto.PageInfo;
import cn.campus.pojo.article.vo.ArticleVO;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by linyanbin on 2018/9/23.
 */
public class ArticleDTOToArticleVOConverter {

    /**
     * 将ArticleDTO对象转成ArticleVO对象，默认内容字段不显示
     * @param articleDTO
     * @return
     */
    public static ArticleVO convert(ArticleDTO articleDTO) {
        if (Objects.isNull(articleDTO)) {
            return null;
        }
        ArticleVO articleVO = new ArticleVO();
        BeanUtils.copyProperties(articleDTO, articleVO);
        articleVO.setUser(articleDTO.getUser()); // 设置用户信息
        articleVO.setArticleContent(null);  // 文章内容字段不显示
        return articleVO;
    }

    /**
     * 将ArticleDTO对象列表转成ArticleVO对象列表
     * @param articleDTOList
     * @return
     */
    public static List<ArticleVO> convert(List<ArticleDTO> articleDTOList) {
        if (Objects.isNull(articleDTOList) || articleDTOList.size() == 0) {
            return null;
        }
        // 使用java8新特性: 流API，方法引用
        return articleDTOList.stream().map(ArticleDTOToArticleVOConverter::convert).collect(Collectors.toList());
    }

    /**
     * 将ArticleDTO分页对象转成ArticleVO分页对象
     * @param dtoPageInfo
     * @return
     */
    public static PageInfo<ArticleVO> convert(PageInfo<ArticleDTO> dtoPageInfo) {
        if (Objects.isNull(dtoPageInfo)) {
            return null;
        }
        PageInfo<ArticleVO> voPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(dtoPageInfo, voPageInfo);
        voPageInfo.setList(convert(dtoPageInfo.getList()));
        return voPageInfo;
    }


}
